package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.SystemInfoUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/23 2:39
 * 总结:
 */
@Service
public class TravelCommendServiceImpl implements ITravelCommendService {
    @Autowired
    private TravelCommendMapper travelCommendMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private  SystemInfoUtils systemInfoUtils;
    @Override
    public PageInfo<TravelCommend> query(QueryObject qo) {
        // 利用第四个参数，取消合理化分页
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), true, false, null);
        List<TravelCommend> list = travelCommendMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(TravelCommend travelCommend) {
        if (travelCommend.getId() != null) {
            // 更新
            travelCommendMapper.updateByPrimaryKey(travelCommend);
        } else {
            // 新增
            travelCommend.setSchedule(new Date());
            travelCommendMapper.insert(travelCommend);
            //系统通知用户
            Travel travel = travelMapper.selectByPrimaryKey(travelCommend.getTravel().getId());
            Integer type = travelCommend.getType();
            if(type==TravelCommend.TYPE_WEEK){
                systemInfoUtils.getWeekCommend(travel);//周通知
            }else if (type==TravelCommend.TYPE_MONTH){
                systemInfoUtils.getMonthCommend(travel);//月通知
            }else if (type==TravelCommend.TYPE_RAIDERS){
                systemInfoUtils.getWStrategyCommend(travel);//攻略通知
            }
        }
    }

    @Override
    public PageInfo<Map> queryForApp(TravelCommendQueryObject qo) {
        // 利用第四个参数，取消合理化分页
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), true, false, null);
        List<Map> list = travelCommendMapper.selectForAppList(qo);
        return new PageInfo<>(list);
    }
}
